package com.cl.pattern.memento.clone;

import com.cl.pattern.memento.basic.Memento;

/**
 * @ClassName Originator
 * @Description 发起人角色---clone方式
 * @Author Small
 * @Date 2020/7/9 10:19
 * @Version 1.0
 **/
public class Originator implements Cloneable {

    private Originator backup;
    /**
     * 内部状态
     */
    private String state = "";

    /**
     * 创建备忘录
     *
     * @return
     */
    public void createMemento() {
        try {
            this.backup = this.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 恢复备忘录
     *
     */
    public void restoreMemento() {
        this.setState(this.backup.getState());
    }



    @Override
    protected Originator clone() throws CloneNotSupportedException {
        return (Originator) super.clone();
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}
